//
//  FCAlertViewController.m
//  FCCommonUse_Example
//
//  Created by WJC on 2018/12/18.
//  Copyright © 2018 64912907. All rights reserved.
//

#import "FCAlertViewController.h"
#import "WJCDefine.h"
@interface FCAlertViewController ()<UIPopoverPresentationControllerDelegate,FCTextViewDelegate>


@property (nonatomic,strong) NSMutableArray <FCAlertAction *>*allAction;
@property(nonatomic) CGRect  initFrame;
@property (nonatomic,strong) UIView *containerView;

@end

@implementation FCAlertViewController

+(FCAlertViewController *)alertCommonWithTitle:(NSString *)title andSubTitle:(NSString *)subTitle andController:(UIViewController *)controller andCommitAction:(void (^)(FCAlertAction *))handler{
    FCAlertViewController *con=[[FCAlertViewController alloc] initWithTitle:title andSubTitle:subTitle andAlertType:FCAlert];
    FCAlertAction *commitAction=[FCAlertAction actionWithTitle:@"确定" style:FCAlertActionDefault handler:handler];
    FCAlertAction *cancelAction=[FCAlertAction actionWithTitle:@"取消" style:FCAlertActionCancel handler:^(FCAlertAction *action) {
        
    }];
    [con addAction:cancelAction];
    [con addAction:commitAction];
    [controller presentViewController:con animated:YES completion:nil];
    return con;
}
+(FCAlertViewController *)alertOnlyCommonWithTitle:(NSString *)title andSubTitle:(NSString *)subTitle andController:(UIViewController *)controller andCommitAction:(void (^)(FCAlertAction *))handler{
    FCAlertViewController *con=[[FCAlertViewController alloc] initWithTitle:title andSubTitle:subTitle andAlertType:FCAlert];
    FCAlertAction *cancelAction=[FCAlertAction actionWithTitle:@"取消" style:FCAlertActionCancel handler:handler];
    [con addAction:cancelAction];
    [controller presentViewController:con animated:YES completion:nil];
    return con;
}


-(instancetype)initWithTitle:(NSString *)title andSubTitle:(NSString *)subTitle andAlertType:(alertType)alertType{
    
    if (self=[super init]) {
            //创建毛玻璃效果
            UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
            //创建毛玻璃视图
            UIVisualEffectView * visualView = [[UIVisualEffectView alloc] initWithEffect:blur];
            visualView.frame = self.view.bounds;
//        visualView.backgroundColor = WJCARGBColor(245, 248, 252,0.5);
            //设置模糊透明度
        visualView.alpha = 0.15;
        self.visualView = visualView;
            //添加到imageView上
            [self.view addSubview:visualView];
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        WJCWeakSelf
        self.popoverPresentationController.delegate = weakSelf;
         self.alertType=alertType;
        switch (alertType) {
            case FCAlert:
                [self alertComonWithTitle:title andSubTitle:subTitle];
                break;
            case FCAlertSheet:
                [self alertSheetWithTitle:title andSubTitle:subTitle];
//                self.modalPresentationStyle = UIModalPresentationFormSheet;
                break;
            case FCAlertTxt:
                [self alertTxtWithTitle:title andSubTitle:subTitle];
                
                break;
            case FCAlertTxtView:
                [self alertTxtWithTitle:title andSubTitle:subTitle];
                
                break;
            case FCAlertContainer:
                [self alertCustomWithContainer];
                
                break;
            case FCAlertContainerSheet:
                [self alertBottomCustomWithContainer];
                break;
            default:
                break;
        }
       
            //3.显示弹出控制器
    }
    
    return self;
}
-(void)addAction:(FCAlertAction *)action{
    if (!_allAction) {
        self.allAction=[NSMutableArray new];
    }
    [self.allAction addObject:action];
    [self setAllAction:self.allAction];
}
-(void)setAllAction:(NSMutableArray<FCAlertAction *> *)allAction{
    _allAction=allAction;
    if (!self.contentView) {
    }
    [self.buttonView removeFromSuperview];
    switch (self.alertType) {
        case FCAlertSheet:
        case FCAlertTxtSheet:
         case FCAlertContainerSheet:{
            self.contentView.width=WJCScreenW;
            self.contentView.backgroundColor=[UIColor whiteColor];
            self.contentView.x=0;
            self.buttonView=[[UIView alloc] initWithFrame:CGRectMake(self.contentView.x, WJCScreenH-WJCTabbarSafeBottomMargin-(56*allAction.count), WJCScreenW, 56*allAction.count)];
            CGFloat y=1;
            for (int i = 0; i <allAction.count; i++) {
                allAction[i].frame=CGRectMake(0, y, WJCScreenW, 55.5);
                y+=56;
                [self.buttonView addSubview: allAction[i]];
                if (allAction[i].style==FCAlertActionDelete) {
                    self.buttonView.y-=8;
                    self.buttonView.height+=8;
                    allAction[i].y+=8;
                    if(i!=0){
                    createLineView(lineView);
                        
                    lineView.backgroundColor= HEXC(0xF5F5F5);
                    lineView.frame=CGRectMake(0, y-57, WJCScreenW, 9);
                    [self.buttonView addSubview: lineView];
                    }
                    y+=8;
                }
            }
            self.contentView.y=WJCScreenH-self.buttonView.height-self.contentView.height;
             
        }
            break;
            
        default:{
            if (allAction.count<3) {
                self.buttonView=[[UIView alloc] initWithFrame:CGRectMake(self.contentView.x, self.contentView.height+self.contentView.y-1, self.contentView.width, 45)];
                self.buttonView.backgroundColor = HEXC(0xD8D8D8);
                for (int i = 0; i <allAction.count; i++) {
                    allAction[i].frame=CGRectMake((self.buttonView.width/allAction.count) *i, 1, (self.buttonView.width/allAction.count)-1, 44);
                    [self.buttonView addSubview: allAction[i]];
                }
                
            }else{
                _contentView.center=CGPointMake(WJCScreenW*0.5, WJCScreenH*0.5-(44*allAction.count)*0.5);
                
                self.buttonView=[[UIView alloc] initWithFrame:CGRectMake(self.contentView.x, self.contentView.height+self.contentView.y, WJCScreenW-128, 44*allAction.count)];
                self.buttonView.backgroundColor = [UIColor whiteColor];
                
                for (int i = 0; i <allAction.count; i++) {
                    allAction[i].frame=CGRectMake(0, 44*i+1, WJCScreenW-128, 43);
                    [self.buttonView addSubview: allAction[i]];
                }
            }
           
        }
            [self setContentViewCorner];
            break;
    }
    self.buttonView.backgroundColor= [UIColor whiteColor];;//[[UIColor whiteColor] colorWithAlphaComponent:0.6];
    if (WJCTabbarSafeBottomMargin>0) {
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, WJCScreenH-WJCTabbarSafeBottomMargin, WJCScreenW, WJCTabbarSafeBottomMargin)];
        view.backgroundColor= [UIColor whiteColor]; //HEXC(0xF5F5F5);
        [self.containerView addSubview:view];
        if(self.alertType == FCAlertSheet||self.alertType == FCAlertTxtSheet||self.alertType == FCAlertContainerSheet)
        self.contentView.y-=WJCTabbarSafeBottomMargin;
    }
    switch (self.alertType) {
        case FCAlertSheet:
        case FCAlertTxtSheet:
         case FCAlertContainerSheet:
            [self.containerView addSubview:self.buttonView];
            break;
            
        default:
            [self.view addSubview:self.buttonView];
            break;
    }
    
    
    
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.containerView.y=self.view.height;
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    switch (self.alertType) {
        case FCAlertSheet:
        case FCAlertTxtSheet:
         case FCAlertContainerSheet:
            self.containerView.y=self.view.height;
            break;
            
        default:
            break;
    }
}
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion{
        switch (self.alertType) {
            case FCAlertSheet:
            case FCAlertTxtSheet:
            case FCAlertContainerSheet:{
                 [UIView animateWithDuration:0.25 animations:^{
                     self.containerView.y=self.view.height;
                    }];
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                     [super dismissViewControllerAnimated:flag completion:completion];
                                 });
                break;
            }
                
            default:
                [super dismissViewControllerAnimated:flag completion:completion];
                break;
        }
   
}
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.popoverPresentationController.delegate=nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openKeyBoard:) name:UIKeyboardWillShowNotification object:nil];
        //键盘将要 回去 的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeKeyBoard:) name:UIKeyboardWillHideNotification object:nil];
    self.containerView.y=self.view.height;
    switch (self.alertType) {
        case FCAlertSheet:
        case FCAlertTxtSheet:
        case FCAlertContainerSheet:{
             [UIView animateWithDuration:0.2 animations:^{
                    self.containerView.y=0;
            //        self.containerView.frame=CGRectMake(0, 0, self.view.width, self.view.height);
                }];
            break;
        }
            
        default:
            break;
    }

}
-(void)openKeyBoard:(NSNotification*)notification {
    self.initFrame=self.contentView.frame;
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
     if (keyboardFrame.size.height+44>WJCScreenH-self.contentView.y-self.contentView.height) {
         WJCWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
       
            weakSelf.contentView.y-=keyboardFrame.size.height-(WJCScreenH-weakSelf.contentView.y-weakSelf.contentView.height)+44;
            if (weakSelf.buttonView) {
                weakSelf.buttonView.y=weakSelf.contentView.height+weakSelf.contentView.y;
            }
        
    }];
     }
    
}

-(void)closeKeyBoard:(NSNotification*)notification {
    WJCWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.contentView.frame=weakSelf.initFrame;
        weakSelf.buttonView.y=weakSelf.contentView.height+weakSelf.contentView.y;
    }];
    
}

-(void)setContentViewCorner{
    [self.contentView.layer.mask removeFromSuperlayer];
    [self.buttonView.layer.mask removeFromSuperlayer];
    UIBezierPath *fieldPath = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(30 , 30)];

    CAShapeLayer *fieldLayer = [[CAShapeLayer alloc] init];

    fieldLayer.frame = self.contentView.bounds;

    fieldLayer.path = fieldPath.CGPath;

    self.contentView.layer.mask = fieldLayer;
    
    UIBezierPath *btnPath = [UIBezierPath bezierPathWithRoundedRect:self.buttonView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(30 , 30)];

    CAShapeLayer *btnLayer = [[CAShapeLayer alloc] init];

    btnLayer.frame = self.buttonView.bounds;

    btnLayer.path = btnPath.CGPath;

    self.buttonView.layer.mask = btnLayer;
}
-(void)alertComonWithTitle:(NSString*)title andSubTitle:(NSString*)subTitle{
    self.titleLabel=[[UILabel alloc] init];
    self.titleLabel.frame=CGRectMake(16, 16, WJCScreenW-160, 200);
    self.titleLabel.font=Bfont15;
    self.titleLabel.numberOfLines=4;
    self.titleLabel.text=title;
    [self.titleLabel sizeToFit];
    self.titleLabel.centerX=(WJCScreenW-128)*0.5;
    self.subTitleLabel=[[UILabel alloc] init];
    self.subTitleLabel.textAlignment=NSTextAlignmentCenter;
    if (subTitle) {
    self.subTitleLabel.frame=CGRectMake(16, 0, WJCScreenW-160, 2000);
    self.subTitleLabel.font=font14;
    self.subTitleLabel.numberOfLines=15;
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:subTitle];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 5.0; // 设置行间距
        paragraphStyle.alignment = NSTextAlignmentCenter; //设置两端对齐显示
        [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedStr.length)];
    self.subTitleLabel.attributedText=attributedStr;
    [self.subTitleLabel sizeToFit];
    self.subTitleLabel.centerX=(WJCScreenW-128)*0.5;
    self.subTitleLabel.y=self.titleLabel.y+self.titleLabel.height+16;
   
    }else{
        self.titleLabel.y=(self.contentView.height- self.titleLabel.height)*0.5;
    }
    if (!title) {
         self.subTitleLabel.y=(self.contentView.height- self.subTitleLabel.height)*0.5;
    }
     [self.contentView addSubview:self.titleLabel];
     [self.contentView addSubview:self.subTitleLabel];
     [self.view addSubview:self.contentView];
}
-(void)alertTxtWithTitle:(NSString*)title andSubTitle:(NSString*)subTitle{
    self.titleLabel=[[UILabel alloc] init];
        //        [self.view addSubview:_titleLabel];
    self.titleLabel.frame=CGRectMake(32, 16, WJCScreenW-192, 200);
    self.titleLabel.font=Bfont15;
    self.titleLabel.numberOfLines=4;
    self.titleLabel.text=title;
    [self.titleLabel sizeToFit];
    self.titleLabel.centerX=(WJCScreenW-128)*0.5;
    self.subTitleLabel=[[UILabel alloc] init];
    self.subTitleLabel.textAlignment=NSTextAlignmentCenter;
    if (!title) {
        self.titleLabel.frame=CGRectMake(0, 0, 0, 0);
    }
    [self.contentView addSubview:self.titleLabel];
    [self.view addSubview:self.contentView];
}
-(void)setAddCustomContainerBlock:(UIView *(^)(UIView *))addCustomContainerBlock{
    _addCustomContainerBlock=addCustomContainerBlock;
    WJCWeakSelf
    if (weakSelf.alertType==FCAlertContainerSheet) {
        [weakSelf alertBottomCustomWithContainer];
    }else{
    [weakSelf alertCustomWithContainer];
    }
}
-(void)alertBottomCustomWithContainer{
    WJCWeakSelf
    if (self.addCustomContainerBlock) {
        UIView *v=self.addCustomContainerBlock(weakSelf.contentView);
        [self.contentView addSubview:v];
        CGFloat vheight =v.height;
        self.contentView.width=v.width;
        self.contentView.height=vheight;
        
        _contentView.size=CGSizeMake(WJCScreenW-128<weakSelf.contentView.width?weakSelf.contentView.width:WJCScreenW-128, vheight>80?(vheight+20+WJCTabbarSafeBottomMargin):100);
        _contentView.y=weakSelf.view.height-self.contentView.height;//v.height-20-WJCTabbarSafeBottomMargin;
        UIBezierPath *fieldPath = [UIBezierPath bezierPathWithRoundedRect:weakSelf.contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(FCScaleHeight(30) , FCScaleHeight(30))];
        
        CAShapeLayer *fieldLayer = [[CAShapeLayer alloc] init];
        
        fieldLayer.frame = weakSelf.contentView.bounds;
        
        fieldLayer.path = fieldPath.CGPath;
        
        weakSelf.contentView.layer.mask = fieldLayer;
        
        _contentView.centerX=WJCScreenW*0.5;
        if (v.width<_contentView.width) {
            v.centerX=_contentView.width*0.5;
        }
        v.y=20;
        [weakSelf.view addSubview:weakSelf.containerView];
        weakSelf.containerView.y= weakSelf.view.height;
        [weakSelf.containerView  removeChildViews];
        [weakSelf.containerView addSubview:weakSelf.contentView];
        v.height = vheight;
        [UIView animateWithDuration:0.25 animations:^{
                           weakSelf.containerView.y=0;
                       }];
    }
}
-(void)alertCustomWithContainer{
    WJCWeakSelf
    if (self.addCustomContainerBlock) {
        UIView *v=self.addCustomContainerBlock(weakSelf.contentView);
        [self.contentView addSubview:v];
        self.contentView.width=v.width;
        self.contentView.height=v.height;
        _contentView.size=CGSizeMake(WJCScreenW-128<weakSelf.contentView.width?weakSelf.contentView.width:WJCScreenW-128, v.height>100?v.height:100);
        _contentView.centerY=WJCScreenH*0.5;
        _contentView.centerX=WJCScreenW*0.5;
        if (v.width<_contentView.width) {
            v.centerX=_contentView.width*0.5;
        }
        if (v.height<_contentView.height) {
            v.centerY=_contentView.height*0.5;
        }
        [weakSelf.view removeChildViews];
        if (self.visualView) {
            [weakSelf.view addSubview:self.visualView];
        }
        
        [weakSelf.view addSubview:weakSelf.contentView];
        [self setContentViewCorner];
    }
}
-(UIView *)containerView{
    if (!_containerView) {
        _containerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        _containerView.backgroundColor=[UIColor clearColor];
    }
    return _containerView;
}
-(void)alertSheetWithTitle:(NSString*)title andSubTitle:(NSString*)subTitle{
    [self.view addSubview:self.containerView];
    self.titleLabel=[[UILabel alloc] init];
        //        [self.view addSubview:_titleLabel];
    self.titleLabel.frame=CGRectMake(32, 8, WJCScreenW-128, 200);
    self.titleLabel.font=Bfont15;
    self.titleLabel.numberOfLines=4;
    self.titleLabel.text=title;
    [self.titleLabel sizeToFit];
    
    self.subTitleLabel=[[UILabel alloc] init];
    self.subTitleLabel.textAlignment=NSTextAlignmentCenter;
    if (subTitle) {
        self.subTitleLabel.frame=CGRectMake(16, 0, WJCScreenW-32, 2000);
        self.subTitleLabel.font=font14;
        self.subTitleLabel.textColor=HEXC(0x646262);
        self.subTitleLabel.numberOfLines=15;
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:subTitle];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 8.0; // 设置行间距
        paragraphStyle.alignment = NSTextAlignmentCenter; //设置两端对齐显示
        [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedStr.length)];
        self.subTitleLabel.attributedText=attributedStr;
        [self.subTitleLabel sizeToFit];
        self.subTitleLabel.centerX=WJCScreenW*0.5;
        self.subTitleLabel.y=self.titleLabel.y+self.titleLabel.height+8;
        
    }else{
        CGFloat height=self.titleLabel.height+self.subTitleLabel.height+self.titleLabel.y+16;
        self.contentView.size=CGSizeMake(WJCScreenW, height>40?height:40);
        
        self.titleLabel.x =(WJCScreenW - self.titleLabel.width)*0.5;
    }
    if (!title) {
        CGFloat height=self.titleLabel.height+self.subTitleLabel.height+self.titleLabel.y+16;
        self.contentView.size=CGSizeMake(WJCScreenW, height>40?height:40);
        self.titleLabel.y=0;
        self.subTitleLabel.y=(self.contentView.height- self.subTitleLabel.height)*0.5;
    }
    CGFloat height=self.titleLabel.height+self.subTitleLabel.height+self.titleLabel.y;
    self.titleLabel.centerX =WJCScreenW*0.5;
    _contentView.size=CGSizeMake(WJCScreenW, height>40?height:40);
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subTitleLabel];
    [self.containerView addSubview:self.contentView];
}
-(void)addTextViewWithConfigurationHandler:(void (^)(FCTextView *))configurationHandler{
    WJCWeakSelf
    if (_alertType==FCAlertTxtView) {
        if (!weakSelf.txtViewArr) {
            weakSelf.txtViewArr=[NSMutableArray new];
        }
        FCTextView *textView=[[FCTextView alloc]init];
        textView.delegate = self;
        [textView.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.and.right.and.bottom.mas_equalTo(0);
        }];
        textView.backgroundColor =  HEXC(0xF5F5F5);
        [weakSelf.txtViewArr addObject:textView];
        [weakSelf setTxtView];
        if (configurationHandler) {
            configurationHandler(textView);
        }
    }
}
-(void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *))configurationHandler{
    WJCWeakSelf
    if (_alertType==FCAlertTxt) {
        if (!weakSelf.txtFieldArr) {
            weakSelf.txtFieldArr=[NSMutableArray new];
        }
        UITextField *field=[[UITextField alloc]init];
//        field.alignmentRectInsets = UIEdgeInsetsMake(0, 8, 0, 8);
        [field setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];
//        [field setValue:[NSNumber numberWithInt:8] forKey:@"paddingRight"];
        
        field.borderStyle=UITextBorderStyleNone;
        field.layer.cornerRadius = 4;
        field.layer.masksToBounds = YES;
        field.backgroundColor =  HEXC(0xF5F5F5);
        [weakSelf.txtFieldArr addObject:field];
        [field addTarget:weakSelf action:@selector(txtEnd:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [weakSelf setTxt];
        if (configurationHandler) {
            configurationHandler(field);
        }
    }
    
    
}
-(void)dealloc{
    
}
-(void)textViewDidEndEditing:(FCTextView *)textView{
    if (self.allAction.count>0) {
        for (FCAlertAction *action in self.allAction) {
            if (action.style!=FCAlertActionCancel) {
                if (action.alertBlock) {
                    action.alertBlock(action);
                }
            }
        }
    }
}
-(void)txtEnd:(UITextField*)textField{
    if (self.allAction.count>0) {
        for (FCAlertAction *action in self.allAction) {
            if (action.style!=FCAlertActionCancel) {
                if (action.alertBlock) {
                    action.alertBlock(action);
                }
            }
        }
    }
}
-(void)setTxtView{
    for (UIView *view in self.contentView.subviews) {
        if([view isKindOfClass:[FCTextView class]]){
            [view removeFromSuperview];
        }
    }
    CGFloat y= self.titleLabel.y+self.titleLabel.height+8;
    for (int i = 0; i <self.txtViewArr.count; i ++) {
        self.txtViewArr[i].frame= CGRectMake(16,y, WJCScreenW-128-32, 70);
        [self.contentView addSubview:self.txtViewArr[i]];
        y+=38;
    }
    if (self.txtViewArr.count==1) {
        self.txtViewArr[0].y+=15;
    }
    self.contentView.y-=120;
    self.contentView.size=CGSizeMake(WJCScreenW-128, y>130?y:130);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44.0f;
}
-(void)setTxt{
    for (UIView *view in self.contentView.subviews) {
        if([view isKindOfClass:[UITextField class]]){
            [view removeFromSuperview];
        }
    }
    CGFloat y= self.titleLabel.y+self.titleLabel.height+8;
    for (int i = 0; i <self.txtFieldArr.count; i ++) {
        self.txtFieldArr[i].frame= CGRectMake(32,y, WJCScreenW-128-64, 30);
        [self.contentView addSubview:self.txtFieldArr[i]];
        y+=38;
    }
    if (self.txtFieldArr.count==1) {
        self.txtFieldArr[0].y+=15;
    }
    self.contentView.y-=80;
    self.contentView.size=CGSizeMake(WJCScreenW-128, y>100?y:100);
}

-(UIView *)contentView{
    if (!_contentView) {
        _contentView=[[UIView alloc] init];
        [self.titleLabel sizeToFit];
        [self.subTitleLabel sizeToFit];
        CGFloat height=self.titleLabel.height+self.subTitleLabel.height+self.subTitleLabel.y+16;
        _contentView.size=CGSizeMake(WJCScreenW-128, height>FCScaleWidth(80)?height:FCScaleWidth(80));
        _contentView.center=CGPointMake(WJCScreenW*0.5, WJCScreenH*0.5-22);
        _contentView.backgroundColor=[UIColor whiteColor];
        
    }
    return _contentView;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    
    if (self.isNoDismiss) return;
    
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    point = [self.contentView.layer convertPoint:point fromLayer:self.view.layer]; //get layer using containsPoint:
    if (![self.contentView.layer containsPoint:point]) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}


@end
